package com.wwdx;

import com.wwdx.service.*;
import com.wwdx.service.impl.MiDuWxMaService;
import com.wwdx.service.impl.MiDuWxService;
import com.wwdx.service.impl.TestWxService;
import com.wwdx.utils.ConstantHolder;
import com.wwdx.wechat.config.AliPayConfig;
import com.wwdx.wechat.config.midu.MdWxConfig;
import com.wwdx.wechat.config.test.TestWxMpConfig;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.*;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author liguoliang
 * @date 2018/5/20 17:02
 */
public abstract class BaseCommons {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    protected AliPayConfig aliPayConfig;
    @Resource
    protected ThreadPoolTaskExecutor taskExecutor;
    @Resource
    protected MiDuWxService miDuWxService;
    @Resource
    protected MdWxConfig mdWxConfig;
    @Resource
    protected MiDuWxMaService miDuWxMaService;
    @Resource
    protected TestWxService testWxService;
    @Resource
    protected TestWxMpConfig testWxMpConfig;
    @Resource
    protected WwdxThemeServiceI wwdxThemeService;
    @Resource
    protected ProductServiceI productService;
    @Resource
    protected WwdxBookServiceI wwdxBookService;
    @Resource
    protected WwdxPersonalServiceI wwdxPersonalService;
    @Resource
    protected WwdxBookClassifyServiceI wwdxBookClassifyService;
    @Resource
    protected PayServiceI payService;
    @Resource
    protected WwdxBannerServiceI wwdxBannerService;
    @Resource
    protected WwdxBookChapterServiceI wwdxBookChapterService;
    @Resource
    protected TaskServiceI taskService;
    @Resource
    protected TemplateMsgTaskServiceI templateMsgTaskService;
    @Resource
    protected ReVersionServiceI reVersionService;
    @Resource
    protected WwdxVipUserServiceI wwdxVipUserService;
    @Resource
    protected WwdxVipProductServiceI wwdxVipProductService;
    @Resource
    protected WwdxOrderServiceI wwdxOrderService;
    @Resource
    protected WwdxCouponServiceI wwdxCouponService;
    @Resource
    protected WwdxCouponLogServiceI wwdxCouponLogService;
    @Resource
    protected WwdxCdKeyServiceI wwdxCdKeyService;
    @Resource
    protected WechatPublishServiceI wechatPublishService;
    @Resource
    protected WvirtualUserServiceI wvirtualUserService;
    @Resource
    protected WvirtualServiceI wvirtualService;
    @Resource
    protected WwdxXiaodServiceI wwdxXiaodService;
    @Resource
    protected WxMenuTreeServiceI wxMenuTreeService;
    @Resource
    protected StatisticsInComeServiceI statisticsInComeService;
    @Resource
    protected WwdxMerchantServiceI wwdxMerchantService;
    @Resource
    protected WwdxPageStatisticsServiceI wwdxPageStatisticsService;
    @Resource
    protected WwdxGiveoneChannelServiceI wwdxGiveoneChannelService;
    @Resource
    protected WwdxGiveoneBookServiceI wwdxGiveoneBookService;
    @Resource
    protected WwdxGiveoneUserServiceI wwdxGiveoneUserService;
    @Resource
    protected WwdxCopywriterServiceI wwdxCopywriterService;
    @Resource
    protected WwdxCommentServiceI wwdxCommentService;
    @Resource
    protected WwdxWeixinUserServiceI wwdxWeixinUserService;
    @Resource
    protected RedisTemplate<String,Object> redisTemplate;
    @Resource
    protected WwdxJdorderServiceI wwdxJdorderService;
    @Resource
    protected WwdxMerchantProductServiceI wwdxMerchantProductService;
    @Resource
    protected WwdxMerchantProductStatisticsServiceI wwdxMerchantProductStatisticsService;
    @Resource
    protected WwdxCardServiceI wwdxCardService;
    @Resource(name = "redisTemplate")
    protected ValueOperations<String,Object> valueOperations;
    @Resource(name = "redisTemplate")
    protected HashOperations<String,String,Object> hashOperations;
    @Resource(name = "redisTemplate")
    protected ListOperations<String,Object> listOperations;
    @Resource(name = "redisTemplate")
    protected SetOperations<String,Object> setOperations;
    @Resource(name = "redisTemplate")
    protected ZSetOperations<String,Object> zSetOperations;
    @Value("#{constantProperties['directory']}")
    protected String directory;
    @Value("#{constantProperties['wechat_url']}")
    protected String wechatUrl;
    @Value("#{constantProperties['domain']}")
    protected String domain;
    @Value("#{constantProperties['log_path']}")
    protected String logPath;
    @Value("#{constantProperties['pic_url']}")
    protected String picUrl;

    /**
     * 获取系统订单号
     */
    protected String makeOrderNum() {
        String key = "system:order:id";
        String time = new DateTime().toString(ConstantHolder.Constant.LOCAL_DATE_PATTERN3);
        return time + String.format("%1$020d", valueOperations.increment(key, 1));
    }

    /**
     * 32位随机数
     */
    protected String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 指定长度随机字符串
     */
    protected String getStringRandom(int length) {
        StringBuilder sb = new StringBuilder();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int data;
        for(int i = 0; i < length; i++) {
            int index = random.nextInt(3);
            switch(index) {
                //0~9的ASCII为48~57
                case 0:
                    //仅仅会生成0~9
                    data = random.nextInt(10);
                    sb.append(data);
                    break;
                //A~Z的ASCII为65~90
                case 1:
                    //保证只会产生65~90之间的整数
                    data = random.nextInt(26) + 65;
                    sb.append((char)data);
                    break;
                //a~z的ASCII为97~122
                case 2:
                    //保证只会产生97~122之间的整数
                    data = random.nextInt(26) + 97;
                    sb.append((char)data);
                    break;
                default:
                    break;
            }
        }
        return sb.toString().toUpperCase();
    }
}